<?php
include "inc/options.php";
putenv('GDFONTPATH=' . realpath('.'));
$font = 'lcd';
$name=addslashes(htmlentities($_GET['name']));
$style=addslashes(htmlentities($_GET['style']));
if ($style==0) { $style=1; }
if (!is_numeric($style)) { die("Error"); }
function errorimg() {
	$im = imagecreate(170, 30);
	$bg = imagecolorallocate($im, 255, 255, 255);
	$textcolor = imagecolorallocate($im, 0, 0, 255);
	imagestring($im, 5, 0, 0, "No stats available", $textcolor);
	header("Content-type: image/png");
	imagepng($im);
}
##############
clearstatcache();
$md5=md5($name.$style);
if (file_exists("cache/".$md5.".jpg")) {
	$diff=time()-filemtime("cache/".$md5.".jpg");
	if ($diff > 43200) {
		unlink("cache/".$md5.".jpg");
		$gen="ok";
	}
	elseif (!$_GET['nocache']) {
		$im = ImageCreateFromjpeg("cache/".$md5.".jpg");
		header ("Content-type: image/jpeg");
		Imagejpeg($im,NULL,100);
	}
}
else {
	$gen="ok";
}
if ($gen=="ok"){
	$fichierSource = "style$style.jpg";
	$im = ImageCreateFromjpeg($fichierSource)
	or die ("Erreur lors de la création de l'image");
	$white = ImageColorAllocate ($im, 255, 255, 255);
	$blue = imagecolorallocate($im, 63, 199, 223);
	mysql_connect(HOST,USER,PASS);
	mysql_select_db(BDD);
	$result=mysql_query("SELECT scores.rank, users.display FROM scores, users WHERE users.user='$name' AND scores.steamid=users.steamid") or die(mysql_error());
	$n=mysql_num_rows($result);
	if ($n==0) {
		errorimg();
		return;
	}
	else {
		$temp=mysql_fetch_array($result);
		if ($temp["display"]) {
			$name=$temp["display"];
		}
		$rang=$temp["rank"];
		if ($rang) {
			if ($style == 1) {
				if (strlen($rang) == 1) { $size=28; $sizex=62; $sizey= 49; }
				if (strlen($rang) == 2) { $size=25; $sizex=55; $sizey= 56; }
				if (strlen($rang) == 3) { $size=20; $sizex=55; $sizey= 58; }
				if (strlen($rang) == 4) { $size=18; $sizex=51; $sizey= 62; }
				if (strlen($rang) == 5) { $size=15; $sizex=51; $sizey= 62; }
				imagettftext($im, $size, 45, $sizex, $sizey, $white, $font, $rang);
				$len=(256*strlen($name))/8;
				########
				$size=11;
				$bbox = imagettfbbox ($size, 0, $font, $name); // Using the name value
				$bbox["left"] = 0- min($bbox[0],$bbox[2],$bbox[4],$bbox[6]);
				$bbox["top"] = 0- min($bbox[1],$bbox[3],$bbox[5],$bbox[7]);
				$bbox["width"] = max($bbox[0],$bbox[2],$bbox[4],$bbox[6]) - min($bbox[0],$bbox[2],$bbox[4],$bbox[6]);
				$bbox["height"] = max($bbox[1],$bbox[3],$bbox[5],$bbox[7]) - min($bbox[1],$bbox[3],$bbox[5],$bbox[7]);
				extract ($bbox, EXTR_PREFIX_ALL, 'bb');
				//check width of the image
				$width = imagesx($im);
				$height = imagesy($im);
				$pad = 0;

				//Params: [Background, fontsize, text-angle, text x-position, text y-position, fontcolor, fonttype, text]
				imagettftext($im, $size, $angle, $width-$bb_width-$pad, $height-$bb_height-$pad+10, $white, $font, $name); 
				#imagettftext($im, 11, 0, 256, 78, $col, $font, $name);
				########
			}
			if ($style == 2){
				if (strlen($rang) == 1) { $size=16; $sizex=35; $sizey= 23; }
				if (strlen($rang) == 2) { $size=15; $sizex=30; $sizey=23; }
				if (strlen($rang) == 3) { $size=15; $sizex=25; $sizey=23; }
				if (strlen($rang) == 4) { $size=15; $sizex=20; $sizey=23; }
				if (strlen($rang) == 5) { $size=11; $sizex=20; $sizey=21; }
				imagettftext($im, $size, 0, $sizex, $sizey, $white, $font, $rang);
				
			}
			if ($style == 3){
				
			}
			if (!$_GET['nocache']) { $temp=Imagejpeg($im,"cache/".$md5.".jpg",100); }
			header ("Content-type: image/jpeg");
			Imagejpeg($im,NULL,100);
		}
		else {
			errorimg();
			return;
			}
		}
	}

?>
